package org.sojex.photo;


import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

import org.sojex.R;
import org.sojex.photo.bean.CookBean;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class MenuListActivity extends Activity {
	Hashtable<String, Object> menuListTable;
	List<String> listHash;
	MyMenu myMenu;
	ListView menuListView;
	TextView noMenu;
	Adapter adapter;
	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menulist);
		listHash = new ArrayList<String>();
		menuListView = (ListView)findViewById(R.id.menuList);
		noMenu = (TextView)findViewById(R.id.nomenulist);
		myMenu = MyMenu.getInstance(this);
		menuListTable = myMenu.getMenu();
		if(menuListTable == null||menuListTable.size()==0){
			menuListView.setVisibility(View.GONE);
		}else{
			initList();
			noMenu.setVisibility(View.GONE);
			adapter = new Adapter();
			menuListView.setAdapter(adapter);
		}
		menuListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Toast.makeText(MenuListActivity.this, "长按删除当前菜品", 1).show();
			}
			
		});
		menuListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				MyMenu.getInstance(MenuListActivity.this).removeMenu(listHash.get(position));
				initList();
				myMenu = MyMenu.getInstance(MenuListActivity.this);
				return false;
			}
		});
	}
	public void initList(){
		Enumeration<String> en = menuListTable.keys();
		for(;en.hasMoreElements();){
			String key = en.nextElement();
			listHash.add(key);
		}
		
	}
	
	class Adapter extends BaseAdapter {
		Holder holder;
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return menuListTable.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			LayoutInflater inflater = LayoutInflater.from(MenuListActivity.this);
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.menulist_row,null);
				holder = new Holder();
				holder.NO = (TextView) convertView
						.findViewById(R.id.NO);
				holder.name = (TextView) convertView
				.findViewById(R.id.foodName);
				holder.num = (TextView) convertView
				.findViewById(R.id.foodNum);
				convertView.setTag(holder);
			} else {
				holder = (Holder) convertView.getTag();
			}
			CookBean cookBean = (CookBean)menuListTable.get(listHash.get(position));
				 int num = position+1;
			holder.NO.setText(num+"");
			holder.name.setText(cookBean.name);
			holder.num.setText(cookBean.num);
			return convertView;
		}
		class Holder {
			TextView NO;
			TextView name;
			TextView num;
		}

	}
	
}
